package com.jcxy.controller;

import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.pojo.Menu;
import com.jcxy.pojo.Role;
import com.jcxy.service.RoleService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 角色管理的控制器
 */
@Controller
@RequestMapping("/roleManage")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private DictionaryMapper dictionaryMapper;

    /**
     * 通过分页获取角色列表
     * @param request
     * @return
     */
    @RequestMapping("/getRoleListByPage.do")
    @ResponseBody
    public ResponseData getRoleListByPage(HttpServletRequest request){

        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = roleService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 删除角色
     * @param role_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteRoleById.do")
    @ResponseBody
    public ResponseData deleteRoleById(Integer role_id,HttpServletRequest request)throws Exception{

        roleService.deleteRoleById(role_id);

        //更新角色缓存
        request.getSession().setAttribute("role",dictionaryMapper.queryRole());

        ResponseData ok = ResponseData.ok();
        return ok;
    }

    /**
     * 跳转到修改页面
     * @param request
     * @return
     */
    @RequestMapping("/toEditPage.do")
    public String toEditPage(HttpServletRequest request){

        Map dto = ParamsDto.getDto(request.getParameterMap());
        Role role = roleService.queryRoleById(dto);

        //将角色id和name存储到请求中，用于回显在页面上
        request.setAttribute("role_id",role.getRole_id());
        request.setAttribute("role_name",role.getRole_name());
        request.setAttribute("pageType",1);//指定为修改页面

        return "roleManage/roleAdd";
    }

    /**
     * 修改角色信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateRoleById.do")
    public String updateRoleById(HttpServletRequest request) throws Exception {

        Map dto = ParamsDto.getDto(request.getParameterMap());
        roleService.updateRole(dto);
        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");
        return "roleManage/roleManage";
    }

    /**
     * 新增角色
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addRole.do")
    public String addRole(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());
        Role role = new Role();
        role.setRole_name(dto.get("role_name").toString());
        roleService.addRole(role);

        //更新角色缓存
        request.getSession().setAttribute("role",dictionaryMapper.queryRole());
        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "roleManage/roleManage";
    }

    /**
     * 通过角色id查询所有菜单以及是否有权
     * @param role_id
     * @return
     */
    @RequestMapping("/getMenuList.do")
    @ResponseBody
    public ResponseData getMenuList(Integer role_id){

        List<Menu> menuList = roleService.queryMenuList(role_id);

        ResponseData ok = ResponseData.ok();
        ok.setData(menuList);
        ok.setCode(0);
        return ok;
    }

    /**
     * 修改角色的权限
     * @param request
     * @throws Exception
     */
    @RequestMapping("/editPower.do")
    @ResponseBody
    public ResponseData editPower(HttpServletRequest request) throws Exception {

        Map dto = ParamsDto.getDto(request.getParameterMap());

        roleService.updateRolePower(dto);

        ResponseData ok = ResponseData.ok();

        if("0".equals(dto.get("isShow"))){
            ok.setMsg("权限已移除");
        }else {
            ok.setMsg("权限已添加");
        }
        return ok;
    }

}
